package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.log.anno.SysOperLog;
import com.atguigu.spzx.log.enums.BusinessTypeEnum;
import com.atguigu.spzx.log.enums.OperTypeEnum;
import com.atguigu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.sys.SysRole;
import com.atguigu.spzx.model.vo.common.R;
//import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Tag(name = "角色管理模块")
//@CrossOrigin(allowCredentials = "true" ,originPatterns = "*" )
@RequestMapping("/admin/system/role")
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;

    @Operation(summary = "给角色分配菜单")
    @PutMapping("assignMenus2Role/{id}")
    public R assignMenus2Role(@PathVariable("id")Long id,
                              @RequestBody List<Long> menuIds){
        sysRoleService.assignMenus2Role(id,menuIds);
        return R.ok();
    }


    @Operation(summary = "查询所有角色和是否分配给用户")
    @GetMapping("listAllRolesAndIsChecked/{userId}")
    public R listAllRolesAndIsChecked(@PathVariable("userId")Long userId){
//        LoginAuthInterceptor.getSysUser().getId() 代表登录的管理员
        List<SysRole> sysRoles = sysRoleService.listAllRolesAndIsChecked(userId);
        return R.ok("items" , sysRoles);
    }
    /**
     * 请求条件：
         *  分页查询：
         *          pageNum/pageSize
         *  条件查询：
         *          角色名称  roleName
         *      ?keyword=xxxx
     * 响应数据：
     *      页面数据需要使用表映射的实体类
     *      分页数据
     */
    @SysOperLog(title = "角色管理模块", bussinessType= BusinessTypeEnum.SELECT,
            operType= OperTypeEnum.MANAGE,isSaveParams=true,isSaveResult=true)
    @Operation(summary = "条件查询角色分页列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum")Integer pageNum ,
                  @PathVariable("pageSize")Integer pageSize,
//                  @RequestParam
                  String keyword){
        PageInfo<SysRole> page =  sysRoleService.list(pageNum,pageSize , keyword);
        return R.ok("page" , page);
    }

    @Operation(summary = "新增角色")
    @PostMapping("save")
    public R save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "删除指定角色")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id){
        sysRoleService.deleteByIds(Arrays.asList(id));
        return R.ok();
    }
    @Operation(summary = "查询指定角色")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return R.ok("item",sysRole);
    }
    @Operation(summary = "更新指定角色")
    @PutMapping("update")
    public R update(@RequestBody SysRole sysRole){
        sysRoleService.update(sysRole);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "查询所有角色")
    @GetMapping("list")
    public R list(){
        List<SysRole> SysRoles =  sysRoleService.list();
        return R.ok("items" , SysRoles);
    }
}
